package com.atguigu.spzx.user.controller;

import com.atguigu.spzx.model.dto.api.ApiUserRegisterDTO;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.api.user.ApiUserInfoVO;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user/userInfo")
@Tag(name = "用户中心")
public class UserInfoController {
    @Resource
    UserInfoService userInfoService;
    @Operation(summary = "注册接口")
    @PostMapping("register")
    public Result register(@RequestBody ApiUserRegisterDTO userRegisterDTO){
        userInfoService.register(userRegisterDTO);
        return Result.ok();
    }
    @Operation(summary = "登录接口")
    @PostMapping("login")
    public Result login(@RequestBody ApiUserRegisterDTO userRegisterDTO){
        String token = userInfoService.login(userRegisterDTO);
        return Result.ok().data(token);
    }
    @Operation(summary = "查询登录用户信息接口")
    @GetMapping("/auth/getCurrentUserInfo")
    public Result getCurrentUserInfo(){
        /*
            查询到的用户数据使用userInfo对象返回可以

            但是前端 如果接收到的返回结果中包含password属性 会导致前端登录状态自动失效....

            所以：自定义VO类封装要返回的数据
         */
        ApiUserInfoVO userInfoVO = userInfoService.getCurrentUserInfo();
        return Result.ok().data(userInfoVO);
    }

    @Operation(summary = "注销接口")
    @GetMapping("/logout")
    public Result logout(@RequestHeader("token")String token){
        //注销：删除redis中的保持的登录状态
        userInfoService.logout(token);
        return Result.ok();
    }
}
